﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using University.Workshop.Business;
using University.Workshop.Model;

namespace University.Workshop.WinApp
{
    public partial class RegisterProductForm : TemplateForm
    {
        private ResourceManager resours;
        private string idProductUpdate;
        private Product productModel = new Product();
        public RegisterProductForm()
        {
            InitializeComponent();
            resours = new ResourceManager(typeof(RegisterAdministrator));
            this.Modify.Visible = false;
            this.textSaleData.Visible = false;
            this.comboBoxGroup.Text = "Lacteos";
        }
        public RegisterProductForm(string idProduct)
        {
            InitializeComponent();
            resours = new ResourceManager(typeof(RegisterAdministrator));
            this.idProductUpdate = idProduct;
            productModel = RegistrationProduct.UpdateByproduct(idProduct);
            this.textIdProduct.Text = productModel.IdProduct;
            this.comboBoxGroup.Text = productModel.Group;
            this.textNameProduct.Text = productModel.NameProduct;
            this.textCost.Text = productModel.CostBuy;
            this.textCostSale.Text = productModel.CostSale;
            this.textAmount.Text = productModel.Amount;
            this.textSaleData.Text = productModel.DateSale;
            this.Register.Visible = false;
            this.dateTimeSale.Visible = false;
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private int Validator()
        {
            errorProviderProduct.Clear();
            int count = 0;
            bool isValid = false;
            isValid = RegistrationProduct.IsValidIdProduct(textIdProduct.Text.Trim());
            if (isValid == false)
            {
                errorProviderProduct.SetError(textIdProduct,"Error");
                count++;
            }            
            isValid = RegistrationProduct.IsValidNameProduct(textNameProduct.Text.Trim());
            if (isValid == false)
            {
                errorProviderProduct.SetError(textNameProduct,"Error");
                count++;
            }
            isValid = RegistrationProduct.IsValidCost(textCost.Text.Trim());
            if (isValid == false)
            {
                errorProviderProduct.SetError(textCost,"Error");
                count++;
            }
            isValid = RegistrationProduct.IsValidCost(textCostSale.Text.Trim());
            if (isValid == false)
            {
                errorProviderProduct.SetError(textCostSale,"Error");
                count++;
            }
            isValid = RegistrationProduct.IsValidCount(textAmount.Text.Trim());
            if(isValid==false)
            {
                errorProviderProduct.SetError(textAmount, "Error");
                count++;
            }
            return count;
        }

        private void Register_Click(object sender, EventArgs e)
        {

            int  isValidator;
            isValidator = Validator();
            if (isValidator >0)
            {
                string caption = this.resours.GetString("messageBox.Caption");
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(this, "Error al introducir los datos", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }
            else 
            {
                int productExist = RegistrationProduct.RegisterProduct(textIdProduct.Text.Trim(), comboBoxGroup.Text.Trim(), textNameProduct.Text.Trim(), textCost.Text.Trim(),textCostSale.Text.Trim(),textAmount.Text.Trim(),dateTimeSale.Text.Trim());
                if (productExist == 0)
                {
                    string caption = this.resours.GetString("messageBox.Caption");
                    MessageBoxButtons buttons = MessageBoxButtons.OK;
                    MessageBox.Show(this, "Ok", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                    this.Close();
                }
                else
                {
                    string caption = this.resours.GetString("messageBox.Caption");
                    MessageBoxButtons buttons = MessageBoxButtons.OK;
                    MessageBox.Show(this, "EL producto existe", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            
                }
            }
        }

        private void Modify_Click_1(object sender, EventArgs e)
        {
            int isValid;
            isValid = Validator();
            if (isValid > 0)
            {
                string caption = this.resours.GetString("messageBox.Caption");
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(this, "Todos los campos son requeridos", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }
            else
            {
                int productExist = RegistrationProduct.ValidateUpdateProducts(idProductUpdate, textIdProduct.Text.Trim(), comboBoxGroup.Text.Trim(), textNameProduct.Text.Trim(), textCost.Text.Trim(), textCostSale.Text.Trim(),textAmount.Text.Trim(),textSaleData.Text.Trim());
                if (productExist == 0)
                {
                    string caption = this.resours.GetString("messageBox.Caption");
                    MessageBoxButtons buttons = MessageBoxButtons.OK;
                    MessageBox.Show(this, "Los datos Fueron cargados corectamente", caption, buttons, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                    this.Close();
                }
                else
                {
                    string caption = this.resours.GetString("messageBox.Caption");
                    MessageBoxButtons buttons = MessageBoxButtons.OK;
                    MessageBox.Show(this, "Existe el Producto", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                 
                }
            }
        }
    }
}
